package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Site;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("SiteDAO")
@Transactional
public class SiteDAOImpl extends AbstractJpaDao<Site> implements SiteDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Site.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public SiteDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Site findSiteById(Integer id) throws DataAccessException {

		return findSiteById(id, -1, -1);
	}

	@Transactional
	public Site findSiteById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSiteById", startResult, maxRows, id);
			return (Site) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Site> findSiteByName(String name) throws DataAccessException {

		return findSiteByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Site> findSiteByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSiteByName", startResult, maxRows, name);
		return new LinkedHashSet<Site>(query.getResultList());
	}

	@Transactional
	public Set<Site> findSiteByNameContaining(String name) throws DataAccessException {

		return findSiteByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Site> findSiteByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSiteByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Site>(query.getResultList());
	}

	@Transactional
	public Set<Site> findSiteByDomain(String domain) throws DataAccessException {

		return findSiteByDomain(domain, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Site> findSiteByDomain(String domain, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSiteByDomain", startResult, maxRows, domain);
		return new LinkedHashSet<Site>(query.getResultList());
	}

	@Transactional
	public Set<Site> findSiteByDomainContaining(String domain) throws DataAccessException {

		return findSiteByDomainContaining(domain, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Site> findSiteByDomainContaining(String domain, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSiteByDomainContaining", startResult, maxRows, domain);
		return new LinkedHashSet<Site>(query.getResultList());
	}

	@Transactional
	public Set<Site> findSiteByDescription(String description) throws DataAccessException {

		return findSiteByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Site> findSiteByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSiteByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Site>(query.getResultList());
	}

	@Transactional
	public Set<Site> findSiteByDescriptionContaining(String description) throws DataAccessException {

		return findSiteByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Site> findSiteByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSiteByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Site>(query.getResultList());
	}

	@Transactional
	public Site findSiteByPrimaryKey(Integer id) throws DataAccessException {
		return findSiteByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Site findSiteByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSiteByPrimaryKey", startResult, maxRows, id);
			return (Site) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Site> findSitesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSitesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Site> findAllSites() throws DataAccessException {

		return findAllSites(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Site> findAllSites(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllSites", startResult, maxRows);
		return new LinkedHashSet<Site>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Site> findSiteByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Site>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Site> findSitesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Site.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Site> sites = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return sites;
	}

	@Transactional
	public Site findSiteByPhysicalPrimaryKey(String name, String domain) throws DataAccessException {
		return findSiteByPhysicalPrimaryKey(name, domain, -1, -1);
	}

	@Transactional
	public Site findSiteByPhysicalPrimaryKey(String name, String domain, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findSiteByPhyscialPrimaryKey", startResult, maxRows, name, domain);
			return (Site) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Site entity) {
		return true;
	}
}
